package com.xiaolin.io;

import java.io.*;

/**
 * @ClassName TestFileInputStream
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/9/25
 * @Version 1.0
 */
public class TestFileInputStream {
    public static void main(String[] args) throws Exception {
        boolean b = readAndWrite("D:\\1file\\haha.png", "D:\\1file\\out.png");
        System.out.println(b);
    }

    /**
     * 读写文件，可读写字符文件，媒体文件
     * @param origin
     * @param dest
     * @return
     */
    public static boolean readAndWrite(String origin,String dest){
        FileInputStream inputStream= null;
        FileOutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(origin);
            //获取整个输入流的字节数估计值，有可能很精确
            //System.out.println(inputStream.available());
            //测试该输入流是否支持mark()和reset方法
            //System.out.println(inputStream.markSupported());
            outputStream=new FileOutputStream(dest);
            byte[] a=new byte[5*1024];  //代表可以存放5+1024个字节
            int read = 0;
            while ((read=inputStream.read(a))!=-1){
                outputStream.write(a,0,read);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

}
